package com.hxzy.servlet.UmsGrowthChangeHistory;

import com.hxzy.entity.UmsGrowthChangeHistory;
import com.hxzy.service.UmsGrowthChangeHistoryService;
import com.hxzy.service.impl.UmsGrowthChangeHistoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/UmsGrowthChangeHistory/insert")
public class insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/UmsGrowthChangeHistory/insert.jsp").forward(req,res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        UmsGrowthChangeHistory history=new UmsGrowthChangeHistory();
        Long memberId=Long.getLong(req.getParameter("memberId"));
        history.setMemberId(memberId);

        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date createTime=sdf.parse(req.getParameter("createTime"));
            history.setCreateTime(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /**
         * 改变类型：0->增加；1->减少
         */
        Integer changeType=Integer.parseInt(req.getParameter("changeType"));
        history.setChangeType(changeType);

        /**
         * 积分改变数量
         */
        Integer changeCount=Integer.parseInt(req.getParameter("changeCount"));
        history.setChangeCount(changeCount);

        /**
         * 操作人员
         */
        String operateMan=req.getParameter("operateMan");
        history.setOperateMan(operateMan);

        /**
         * 操作备注
         */
        String operateNote=req.getParameter("operateNote");
        history.setOperateNote(operateNote);

        /**
         * 积分来源：0->购物；1->管理员修改
         */
        Integer sourceType=Integer.parseInt(req.getParameter("sourceType"));
        history.setSourceType(sourceType);

        UmsGrowthChangeHistoryService service=new UmsGrowthChangeHistoryServiceImpl();
        boolean insert = service.insert(history);

        PrintWriter out=res.getWriter();
        out.println( insert? "新增成功": "新增失败");
        //输出刷新
        out.flush();
        out.close();
    }
}
